package org.metasketch.ham.diagrams;

import org.metasketch.diagram.javafx.graphics.DefaultLink;
import org.metasketch.diagram.javafx.graphics.HorizontalLinkLabelPosition;
import org.metasketch.diagram.javafx.graphics.LinkEndBuilder;
import org.metasketch.diagram.javafx.graphics.PositionedLabel;
import org.metasketch.diagram.javafx.graphics.VerticalLinkLabelPosition;
import org.metasketch.editor.links.LineSection;
import org.metasketch.editor.links.LinkEndType;
import org.metasketch.ham.model.HumanActivityParticipationRelationLink;
import org.metasketch.ham.model.HumanActivityParticipationRelationType;
import org.metasketch.ham.model.HumanActivityRelationLink;
import org.metasketch.ham.model.HumanActivityRelationType;
import org.metasketch.ham.model.HumanParticipationElement;

public class HybridRelationshipLink extends DefaultLink {

	PositionedLabel label = new PositionedLabel();

	public HybridRelationshipLink() {
		{
			// PositionedLabel label1 = new PositionedLabel();
			label.getLabel().setText("Center,Center");
			label.setHorizontal(HorizontalLinkLabelPosition.CENTER);
			label.setVertical(VerticalLinkLabelPosition.TOP);
			labels.add(label);
		}

	}

	@Override
	protected void refreshModelGraphic() {
		super.refreshModelGraphic();

		if (semantic == null)
			return;

		HumanActivityParticipationRelationLink relation = (HumanActivityParticipationRelationLink) semantic;
		HumanActivityParticipationRelationType type = relation.getType();
		HumanParticipationElement partElement = relation.getTarget();

		if (type == HumanActivityParticipationRelationType.USES) {

			// LinkEndBuilder.buildEnd(this, getLastSection(),
			// LinkEndType.ARROW,
			// false);
			setDashedLines();

		} else if (type == HumanActivityParticipationRelationType.INPUT) {

			LinkEndBuilder.buildEnd(this, getFirstSection(), LinkEndType.ARROW,
					true);
			setDashedLines();
		} else if (type == HumanActivityParticipationRelationType.OUTPUT) {

			LinkEndBuilder.buildEnd(this, getLastSection(), LinkEndType.ARROW,
					false);
			setDashedLines();

		} else if (type == HumanActivityParticipationRelationType.INITIATES) {

			LinkEndBuilder.buildEnd(this, getLastSection(),
					LinkEndType.FILLEDSQUARE, false);

		} else if (type == HumanActivityParticipationRelationType.EXECUTES) {
			LinkEndBuilder.buildEnd(this, getLastSection(),
					LinkEndType.FILLEDCIRCLE, false);
		}

		// LinkEndBuilder.buildEnds(this);
		label.getLabel().setText(type.toString());

		// getChildren().add(label);
	}

	private void setDashedLines() {
		for (LineSection line : lines) {
			line.setStyle("-fx-stroke: black; -fx-stroke-width: 2;   -fx-stroke-dash-array: 12 6;    -fx-stroke-dash-offset: 6;     -fx-stroke-line-cap: butt;");

		}
	}

	// public void refreshLineDecorations() {
	// // int rest = lines.size() % 2;
	// // if (rest == 0) {
	// // // get the middle control point
	// // int index = lines.size() / 2 - 1;
	// // LineSection line = lines.get(index);
	// // int x = (int) line.getEndX();
	// // int y = (int) line.getEndY();
	// //
	// // x -= label.getWidth() / 2;
	// // y -= 20;
	// //
	// // label.setTranslateX(x);
	// // label.setTranslateY(y);
	// //
	// // } else {
	// // int index = lines.size() / 2;
	// // LineSection line = lines.get(index);
	// //
	// // int x = (int) (line.getStartX() + (line.getEndX() - line
	// // .getStartX()) * 0.5);
	// // int y = (int) (line.getStartY() + (line.getEndY() - line
	// // .getStartY()) * 0.5);
	// //
	// // x -= label.getWidth() / 2;
	// // y -= 20;
	// //
	// // label.setTranslateX(x);
	// // label.setTranslateY(y);
	// // }
	//
	// }

}
